package com.ebocy.hammer.core.task;

import java.util.Iterator;

import com.ebocy.hammer.core.GameCache;
import com.ebocy.hammer.core.constants.GameConstants;
import com.ebocy.hammer.core.domain.GamePlayer;
import com.ebocy.hammer.core.service.GamePlayerService;
/**
 * 定期执行心跳时间检查，超过指定时间，玩家被迫下线
 * @author HouQingrong
 * Jul 17, 2012
 */
public class HeartBeatTask implements Runnable {
	GamePlayerService gamePlayerService = GamePlayerService.getInstance();
	@Override
	public void run() {
		try {
			long currentTimeMillis = System.currentTimeMillis();
			Iterator<GamePlayer> it = GameCache.getInstance().getOnlineGamePlayers().values().iterator();
			while(it.hasNext()) {
				GamePlayer gamePlayer = it.next();
				if (gamePlayer.heartBeatTimestamp > 0) {
					long expiredTime = gamePlayer.heartBeatTimestamp + GameConstants.CLIENT_TIME_OUT_MINUTE * 60 * 1000;
					if (expiredTime <= currentTimeMillis) {
						gamePlayerService.exitGame(gamePlayer);
						System.out.println("踢出 玩家== " + gamePlayer.getGamePlayerId());
					}
				}
			}
		} catch (Exception e) {
			System.out.println("心跳出错###");
			e.printStackTrace();
		}
		
	}

}
